package cjlu.ea.h.controller;


import cjlu.ea.h.common.ByzgCheck;
import cjlu.ea.h.common.R;
import cjlu.ea.h.common.TimeUtils;
import cjlu.ea.h.entity.ByszgshbLsb;
import cjlu.ea.h.entity.OpTime;
import cjlu.ea.h.service.F101ByszgshbLsbService;
import cjlu.ea.h.service.OpTimeService;
import cjlu.ea.h.service.SUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.netty.util.internal.SuppressJava6Requirement;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-07
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/h/byszgshb-lsb")
public class F101ByszgshbLsbController {
    @Autowired
    F101ByszgshbLsbService byszgshbLsbService;
    @Autowired
    SUserService userService;
    @Autowired
    OpTimeService opTimeService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    ByzgCheck byzgCheck;

    //获取毕业生数据
    @GetMapping("/getbyssj")
    public R getbyssj(HttpServletRequest request){
        String roles = userService.getUserRoles(userService.getToken(request));
        List<ByszgshbLsb> list;
        if(!roles.equals("校级")){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("xy",roles);
             list = byszgshbLsbService.list(wrapper);
        }else{
             list = byszgshbLsbService.list(null);
        }

        System.out.println(list);
        return R.ok().data("list",list);
    }

    //分页条件查询
    @PostMapping("/getPagebyssjBytj/{current}/{limit}")
    public R getPagebyssjBytj(@PathVariable Long current,@PathVariable Long limit,
                              @RequestBody(required = false) ByszgshbLsb byszgshbLsb,HttpServletRequest request){
        String roles = userService.getUserRoles(userService.getToken(request));
        List<Object> res;
        if(roles.equals("校级")){
            res = byszgshbLsbService.getbyssj(byszgshbLsb,current,limit,null);
        }else{
            res = byszgshbLsbService.getbyssj(byszgshbLsb,current,limit,roles);
        }

        return R.ok().data("list",(List<ByszgshbLsb>)res.get(0)).data("total",(Long)res.get(1));
    }


    @GetMapping("/getAllPagebyssjBytj/{current}/{limit}")
    public R getAllPagebyssjBytj(@PathVariable Long current,@PathVariable Long limit,HttpServletRequest request){

        String roles = userService.getUserRoles(userService.getToken(request));
        List<ByszgshbLsb> list;
        Page<ByszgshbLsb> page = new Page<>(current,limit);
        if(!roles.equals("校级")){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("xy",roles);
            byszgshbLsbService.page(page,wrapper);
        }else{
            byszgshbLsbService.page(page,null);
        }

        //分页显示

        //数据查询获取
        list = page.getRecords();
        Long total = page.getTotal();

        return R.ok().data("list",list).data("total",total);
    }


    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable Integer id){

        OpTime opTime = opTimeService.getOne(null);
        boolean isAva = TimeUtils.compareDate(opTime.getStartTime(), opTime.getEndTime());
        log.info("进入deleteById: 可操作时间{}--{} ,当前状态{}",opTime.getStartTime(),opTime.getEndTime(),isAva);
        if (isAva){
            log.info("进行删除操作...");
            boolean result = byszgshbLsbService.removeById(id);
            if (result){
                return R.ok().message("删除成功");
            }else{
                return R.error().message("删除失败");
            }
        }

            log.info("不在操作时间");
            return R.error().message("删除失败");




    }

    @GetMapping("getbysById/{id}")
    public R getbysById(@PathVariable Integer id){
        return R.ok().data("bys",byszgshbLsbService.getById(id));
    }

    //修改按钮
    @PostMapping("update")
    public R updatebysById(@RequestBody ByszgshbLsb byszgshbLsb){
        List<ByszgshbLsb> list = byzgCheck.getInsertOrUpdateList(byszgshbLsb);
        if (list!=null){
            byszgshbLsbService.saveOrUpdateBatch(list);
        }
        return R.ok();
    }
    //新增按钮
    @PostMapping("saveOrUpdateOne")
    public R saveOrUpdateBysById(@RequestBody ByszgshbLsb byszgshbLsb){
        List<ByszgshbLsb> list = byzgCheck.getInsertOrUpdateList(byszgshbLsb);
        if (list!=null){
            byszgshbLsbService.saveOrUpdateBatch(list);
        }
        return R.ok();
    }


}

